package cn.chinaflame.pagodas.service;

import cn.chinaflame.pagodas.enums.CodeEnum;
import cn.chinaflame.pagodas.handle.HandlerException;
import cn.chinaflame.pagodas.utils.ParamPreconditions;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * @author created by boming
 * @date 2018年1月16日 下午4:37:15
 */
public class JdbcQueryV2<E> {


    private static final Logger log = LoggerFactory.getLogger(JdbcQueryV2.class);

    private List<E> content;

    public JdbcQueryV2(final String sql, final JdbcTemplate jdbcTemplate, final Class<E> c) {
        ParamPreconditions.checkNotNull(jdbcTemplate, CodeEnum.FAIL.getCode(), "jdbctemplate is null");
        ParamPreconditions.checkNotEmpty(sql, CodeEnum.FAIL.getCode(), "查询sql不能为空");
        content = Lists.newArrayList();
//        log.info("查询sql:{}", sql);
        List<Map<String, Object>> result = jdbcTemplate.queryForList(sql);
        try {
            for (int i = 0; i < result.size(); i++) {
                E e = c.newInstance();
                for (Entry<String, Object> entry : result.get(i).entrySet()) {
                    Field f = e.getClass().getDeclaredField(getProperty(entry.getKey()));
                    if (f.getType().getSimpleName().equals("BigDecimal") && entry.getValue() != null) {
                        BigDecimal b = new BigDecimal(String.valueOf(entry.getValue()));
                        f.setAccessible(true);
                        f.set(e, b);
                    } else if (f.getType().getSimpleName().equals("Integer") && entry.getValue() != null) {
                        Integer b = Integer.valueOf(String.valueOf(entry.getValue()));
                        f.setAccessible(true);
                        f.set(e, b);
                    } else if (f.getType().getSimpleName().equals("int") && entry.getValue() != null) {
                        Integer b = Integer.valueOf(String.valueOf(entry.getValue()));
                        f.setAccessible(true);
                        f.set(e, b);
                    } else {
                        if (entry.getValue() != null) {
                            f.setAccessible(true);
                            f.set(e, String.valueOf(entry.getValue()));
                        }
                    }
                }
                content.add(e);
            }
        } catch (ReflectiveOperationException e) {
            e.printStackTrace();
            throw new HandlerException(CodeEnum.FAIL.getCode(), "jdbc查询异常", "jdbc查询异常", e);
        }
    }

    public JdbcQueryV2() {
    }

    private static String getProperty(String column) {
        while (column.contains("_")) {
            int a = column.indexOf("_");
            column = column.substring(0, a + 1) + column.substring(a + 1, a + 2).toUpperCase() + column.substring(a + 2, column.length());
            column = column.replaceFirst("_", "");
        }
        return column;
    }

    public List<E> getContent() {
        return content;
    }

    public void setContent(List<E> content) {
        this.content = content;
    }


}
